package com.aaron.socket.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

import sun.misc.Lock;

import com.aaron.socket.utils.I18MessageUtils;
import com.aaron.socket.utils.PropertiesUtils;

/**
 * 
 * @类名称：SocketClient
 * @类描述：
 * @创建人：aaron
 * @修改人：
 * @修改时间：2014年5月11日 下午3:04:42
 * @修改备注：
 * @version:
 */
public class SocketClient
{

    /**
     * 定义日志类
     */
    private final static Logger logger = Logger.getLogger(SocketClient.class
            .getName());

    /**
     * 主机
     */
    private String host;

    /**
     * 任务编号
     */
    private int taskId;

    /**
     * 线程池对象
     */
    private ExecutorService executorService;
    
    /**
     * 确定一个唯一锁对象
     */
    private String lock = new String("lock");

    /**
     * 端口
     */
    private int prot;

    public SocketClient(int taskId) throws IOException
    {
        logger.log(Level.INFO,
                getMessage("utils.message.log.startInitClientData"));

        this.taskId = taskId;
        // 读属性
        prot = Integer.parseInt(readProperty("port"));
        executorService = Executors.newCachedThreadPool();
        executorService.execute(createTask(taskId));

    }

    private Runnable createTask(final int taskId)
    {
        return new Runnable()
        {
            private Socket socket = null;

            public void printInfo(String msg)
            {
                logger.log(Level.INFO, "MSG：" + msg);
            }

            @Override
            public void run()
            {
                synchronized (lock)
                {
                    
                // TODO Auto-generated method stub
                logger.log(Level.INFO,
                        getMessage("utils.message.log.excuteClient") + taskId );
                try
                {
                    socket = new Socket(host, prot);
                    OutputStream socketOut = socket.getOutputStream();
                    socketOut.write("shutdown\r\n".getBytes());

                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(socket.getInputStream()));
                    String msg = null;
                    while (null != (msg = br.readLine()))
                    {
                        this.printInfo(msg);
                    }
                } catch (IOException e)
                {
                    logger.log(Level.SEVERE,
                            getMessage("utils.message.log.ioException"), e);
                }
            }

            }
        };

    }

    /**
     * 
     * @方法描述：
     * @方法名：readProperty
     * @参数：@param key
     * @返回：void
     * @exception
     * @since
     */
    private static String readProperty(String key)
    {
        return PropertiesUtils.getInstance().readProperty(key);
    }

    /**
     * 
     * @方法描述：
     * @方法名：getMessage
     * @参数：@param key
     * @参数：@return
     * @返回：String
     * @exception
     * @since
     */
    private static String getMessage(String key)
    {
        return I18MessageUtils.getInstance().getMessage(key);
    }

}
